package com.letv.ads.http;

import android.content.Context;
import com.letv.adlib.managers.debugger.ARKDebugManager;
import com.letv.adlib.managers.status.ad.AdStatusManager;
import com.letv.adlib.managers.status.ad.IAdDownloadInformer;
import com.letv.adlib.model.ad.common.AdInfo;
import com.letv.adlib.model.ad.common.AdInfoWrapper;
import com.letv.adlib.model.ad.common.VideoCombineBean;
import com.letv.adlib.model.services.CommonAdDataService;
import com.letv.adlib.model.utils.SoMapperKey;
import com.letv.adlib.sdk.types.AdElement;
import com.letv.adlib.sdk.types.AdElementMime;
import com.letv.adlib.sdk.types.ArkAdReqParam;
import com.letv.adlib.sdk.utils.IPlayerStatusDelegate;
import com.letv.ads.AdsManager;
import com.letv.ads.util.ClientInfoUtil;
import java.util.ArrayList;

public class AdsHttpApi {

    /**
     * 调用方舟SDK得到广告，同步耗时
     */
    private static ArrayList<AdElementMime> getAd(ArkAdReqParam reqParam) {
        if (reqParam == null) {
            ARKDebugManager.showArkErrorInfo("广告参数异常", null);
            return null;
        }
        ArrayList<AdElementMime> adInfos = null;
        try {
            adInfos = CommonAdDataService.getAdData(reqParam);
        } catch (Exception e) {
            e.printStackTrace();
            ARKDebugManager.showArkErrorInfo("获取广告数据出错", e);
        }

        return adInfos;
    }

    /**
     * 请求前贴专用   调用方舟SDK得到广告，同步耗时
     */
    private static AdInfo getFrontAd(ArkAdReqParam reqParam) {
        if (reqParam == null) {
            ARKDebugManager.showArkErrorInfo("广告参数异常", null);
            return null;
        }
        AdInfo adWithInformer = null;
        try {
            adWithInformer = CommonAdDataService.getAdWithInformer(reqParam);
        } catch (Exception e) {
            e.printStackTrace();
            ARKDebugManager.showArkErrorInfo("获取广告数据出错", e);
        }
        return adWithInformer;
    }

    /**
     * 请求前贴片广告
     */
    public static ArrayList<AdElementMime> getFrontAd(Context context, String cid, String pid, String vid, String mmsid, String uuid, String uid, String vlen, String py, String ty, IPlayerStatusDelegate iPlayerStatusDelegate) {

        ArkAdReqParam reqParam = ClientInfoUtil.getVideoPlayerInfo(context, cid, pid, vid, mmsid, uuid, uid, vlen, py, ty, iPlayerStatusDelegate);

        reqParam.adReqType = ArkAdReqParam.AdReqType.REQ_Vod;
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.PREROLL;

        return getAd(reqParam);
    }

    /**
     * modified by zengsonghai 20140304
     * 请求多个前贴片广告 needMultiPreRoll  是：true 否：false;
     */
    public static AdInfo getFrontAd(Context context, String cid, String pid, String vid, String mmsid, String uuid, String uid, String vlen, String py, String ty, IPlayerStatusDelegate iPlayerStatusDelegate, boolean needMultiPreRoll, boolean isVipVideo, boolean disableAvd, boolean isHotVideo) {
//		vid = "letvtest01";
//		pid = "group_1";
        ArkAdReqParam reqParam = ClientInfoUtil.getVideoPlayerInfo(context, cid, pid, vid, mmsid, uuid, uid, vlen, py, ty, iPlayerStatusDelegate, isVipVideo);
        if (reqParam.dynamicParams != null) {
            reqParam.dynamicParams.put("qrContentText", AdsManager.getInstance().getLetvQRCodeUrl());
        }
        reqParam.isFromPush = AdsManager.getInstance().isFromPush();
        reqParam.isFromQrCode = AdsManager.getInstance().isFromQRCode();
        reqParam.isHotVideo = isHotVideo;
        reqParam.isDisableAVD = disableAvd;//AVD开关
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.PREROLL;// 前贴片
        reqParam.needMultiPreRoll = needMultiPreRoll;
        return getFrontAd(reqParam);
    }

    public static AdInfo getFrontAd(Context context, String cid, String pid, String vid, String mmsid, String uuid, String uid, String vlen, String py, String ty, IPlayerStatusDelegate iPlayerStatusDelegate,
                                    boolean needMultiPreRoll, boolean isVipVideo, boolean disableAvd, boolean isHotVideo, boolean isOfflineVideo) {
        ArkAdReqParam reqParam = ClientInfoUtil.getVideoPlayerInfo(context, cid, pid, vid, mmsid, uuid, uid, vlen, py, ty, iPlayerStatusDelegate, isVipVideo);
        if (reqParam.dynamicParams != null) {
            reqParam.dynamicParams.put("qrContentText", AdsManager.getInstance().getLetvQRCodeUrl());
        }
        reqParam.isFromPush = AdsManager.getInstance().isFromPush();
        reqParam.isFromQrCode = AdsManager.getInstance().isFromQRCode();
        reqParam.isHotVideo = isHotVideo;
        reqParam.isOfflineAd = isOfflineVideo;
        reqParam.isDisableAVD = disableAvd;//AVD开关
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.PREROLL;// 前帖片
        reqParam.needMultiPreRoll = needMultiPreRoll;
        return getFrontAd(reqParam);
    }

    /**
     * 离线缓存的广告
     *
     * @param context
     * @param cid
     * @param pid
     * @param vid
     * @param mmsid
     * @param uuid
     * @param uid
     * @param vlen
     * @param py
     * @param ty
     * @param iAdDownloadInformer
     * @param needMultiPreRoll
     * @param isVipVideo
     * @param disableAvd
     * @param isHotVideo
     * @param isOfflineVideo
     * @return
     */
    public static AdInfoWrapper getOfflineCachedAd(Context context, String cid, String pid, String vid, String mmsid, String uuid, String uid, String vlen, String py, String ty, IAdDownloadInformer iAdDownloadInformer,
                                                   boolean needMultiPreRoll, boolean isVipVideo, boolean disableAvd, boolean isHotVideo, boolean isOfflineVideo) {
        ArkAdReqParam reqParam = ClientInfoUtil.getVideoPlayerInfo(context, cid, pid, vid, mmsid, uuid, uid, vlen, py, ty, null, isVipVideo);
        if (reqParam.dynamicParams != null) {
            reqParam.dynamicParams.put("qrContentText", AdsManager.getInstance().getLetvQRCodeUrl());
        }
        reqParam.isFromPush = AdsManager.getInstance().isFromPush();
        reqParam.isFromQrCode = AdsManager.getInstance().isFromQRCode();
        reqParam.isHotVideo = isHotVideo;
        reqParam.isOfflineAd = isOfflineVideo;
        reqParam.isDisableAVD = disableAvd;//AVD开关
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.PREROLL;// 前帖片
        reqParam.needMultiPreRoll = needMultiPreRoll;

        return CommonAdDataService.requestOfflineAd(reqParam, iAdDownloadInformer);
    }


    /**
     * 上报付费影片广告？
     *
     * @param context
     * @param cid
     * @param pid
     * @param vid
     * @param mmsid
     * @param uuid
     * @param uid
     * @param vlen
     * @param py
     * @param ty
     * @param iPlayerStatusDelegate
     * @param needMultiPreRoll
     * @param isVipVideo
     * @param disableAvd
     */
    public static void getFrontVipAd(Context context, String cid, String pid, String vid, String mmsid, String uuid, String uid,
                                     String vlen, String py, String ty, IPlayerStatusDelegate iPlayerStatusDelegate,
                                     boolean needMultiPreRoll, boolean isVipVideo, boolean disableAvd, String cuePointType) {
        ArkAdReqParam reqParam = ClientInfoUtil.getVideoPlayerInfo(context, cid, pid, vid, mmsid, uuid, uid, vlen, py, ty, iPlayerStatusDelegate, isVipVideo);
        reqParam.isFromQrCode = AdsManager.getInstance().isFromQRCode();
        reqParam.isDisableAVD = disableAvd;
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.PREROLL;
        if (AdElement.CuePointType.Pause.value() == Integer
                .valueOf(cuePointType)) {
            reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.PAUSE;
        }
        reqParam.needMultiPreRoll = needMultiPreRoll;
        reqParam.isTryLook = true;
        if (reqParam.dynamicParams != null) {
            reqParam.dynamicParams.put("qrContentText", AdsManager.getInstance().getLetvQRCodeUrl());
        }
        getFrontAd(reqParam);
    }

    /**
     * 请求直播前贴片广告
     */
    public static ArrayList<AdElementMime> getLiveFrontAd(Context context, String streamUrl, String uuid, String uid, String py, String ty, IPlayerStatusDelegate iPlayerStatusDelegate) {
        ArkAdReqParam reqParam = ClientInfoUtil.getLivePlayerInfo(context, streamUrl, uuid, uid, ty, iPlayerStatusDelegate);
        reqParam.isFromQrCode = AdsManager.getInstance().isFromQRCode();
        reqParam.isFromQrCode = AdsManager.getInstance().isFromQRCode();
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.PREROLL;//前贴片
        if (reqParam.dynamicParams != null) {
            reqParam.dynamicParams.put("qrContentText", AdsManager.getInstance().getLetvQRCodeUrl());
        }

        return getAd(reqParam);
    }

    /**
     * 请求直播前贴片广告
     */
    public static ArrayList<AdElementMime> getLiveFrontAd(Context context, String streamUrl, String uuid, String uid, String py, String ty, IPlayerStatusDelegate iPlayerStatusDelegate, boolean needMultiPreRoll, boolean isTrylook) {
        ArkAdReqParam reqParam = ClientInfoUtil.getLivePlayerInfo(context, streamUrl, uuid, uid, ty, iPlayerStatusDelegate);
        reqParam.isFromQrCode = AdsManager.getInstance().isFromQRCode();
        reqParam.isFromPush = AdsManager.getInstance().isFromPush();
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.PREROLL;// 前贴片
        reqParam.needMultiPreRoll = needMultiPreRoll;
        reqParam.isTryLook = isTrylook;
        if (reqParam.dynamicParams != null) {
            reqParam.dynamicParams.put("qrContentText", AdsManager.getInstance().getLetvQRCodeUrl());
        }
        return getAd(reqParam);
    }

    /**
     * 请求暂停广告
     */
    public static ArrayList<AdElementMime> getPauseAd(Context context, String cid, String pid, String vid, String mmsid, String uuid, String uid, String vlen, String py, String ty, IPlayerStatusDelegate iPlayerStatusDelegate) {
        ArkAdReqParam reqParam = ClientInfoUtil.getVideoPlayerInfo(context, cid, pid, vid, mmsid, uuid, uid, vlen, py, ty, iPlayerStatusDelegate);
        reqParam.isFromQrCode = AdsManager.getInstance().isFromQRCode();
        reqParam.isFromPush = AdsManager.getInstance().isFromPush();
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.PAUSE;// 暂停
        if (reqParam.dynamicParams != null) {
            reqParam.dynamicParams.put("qrContentText", AdsManager.getInstance().getLetvQRCodeUrl());
        }

        return getAd(reqParam);
    }

    /**
     * 请求移动端开机启动图广告
     */
    public static ArrayList<AdElementMime> getBeginImgAd(Context context) {
        ArkAdReqParam reqParam = ClientInfoUtil.getBeginInfo(context);
        ArrayList<AdElementMime> list = getAd(reqParam);
        if (list != null && list.size() > 0) {
            new AdStatusManager(list.get(0)).onAdClosed();
        }
        return list;
    }

    /**
     * 请求移动端焦点图第三帧广告
     */
    public static ArrayList<AdElementMime> getFocusImgAd(Context context) {
        ArkAdReqParam reqParam = ClientInfoUtil.getFocusInfo(context);
        return getAd(reqParam);
    }

    /**
     * 请求移动端详情页顶部banner广告
     */
    public static ArrayList<AdElementMime> getTopBannerAd(Context context, String cid, String pid) {
        ArkAdReqParam reqParam = ClientInfoUtil.getBannerInfo(context, cid, pid);
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.BANNER;// 焦点图

        return getAd(reqParam);
    }

    /**
     * 请求搜索框广告
     */
    public static ArrayList<AdElementMime> getSearchKeyWord(Context context) {
        ArkAdReqParam reqParam = ClientInfoUtil.getFocusInfo(context);
        //搜索框关键词广告
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.KEYWORD;
        return getAd(reqParam);
    }

    /**
     * 请求半屏底部广告，包括文字链和半屏图片
     */
    public static ArrayList<AdElementMime> getBottomAdData(Context context,
            int cid, long aid, long vid, String mmsid) {
        ArkAdReqParam reqParam = ClientInfoUtil.getClientInfo(context);
        // 文字链广告
        reqParam.adZoneType = ArkAdReqParam.LetvAdZoneType.TEXTLINK;
        reqParam.dynamicParams.put(SoMapperKey.CID, cid + "");
        return getAd(reqParam);
    }

    public static VideoCombineBean getRemoteCombineResult(String ahl, String vl, String atl, boolean isUseCde) {
        return CommonAdDataService.getRemoteCombineResult(ahl, vl, atl, isUseCde);
    }

    /**
     * 给广告发送反馈日志，
     *
     * @param uniqueId 传入手机号
     */
    public static void sendFeedbackToAd(String uniqueId) {
        CommonAdDataService.sendDebugLog(uniqueId);
    }
}
